package com.wude.guild;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.mc.developmentkit.utils.ToastUtil;
import com.mc.developmentkit.views.SimpleFooter;
import com.mc.developmentkit.views.SimpleHeader;
import com.mc.developmentkit.views.SpringView;
import com.umeng.analytics.MobclickAgent;
import com.wude.bean.CouponListBean;
import com.wude.guild.adapter.ListCouponAdapter;
import com.wude.guild.servercore.mints.TitleBaseActivity;
import com.wude.http.HttpCom;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 优惠券列表
 */
public class ListCouponActivity extends TitleBaseActivity implements View.OnClickListener {
    @BindView(R.id.list_view)
    ListView listView;
    @BindView(R.id.spring_view)
    SpringView springView;

    private ListCouponAdapter couponAdapter;
    private List<CouponListBean.DataBean> list = new ArrayList<>();
    private int limit = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_coupon);
        ButterKnife.bind(this);

        setHeaderTitle("优惠券");
        TextView rightTextView = getTitleHeaderBar().getRightTextView();
        rightTextView.setText("我的优惠券");
        rightTextView.setOnClickListener(this);

        couponAdapter = new ListCouponAdapter(this);
        listView.setAdapter(couponAdapter);
        initdata();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    private void initdata() {
        springView.setType(SpringView.Type.FOLLOW);
        springView.setListener(onFreshListener);
        springView.setHeader(new SimpleHeader(this));
        springView.setFooter(new SimpleFooter(this));
        initAndReflsh();
    }

    SpringView.OnFreshListener onFreshListener = new SpringView.OnFreshListener() {
        @Override
        public void onRefresh() {
            initAndReflsh();
        }

        @Override
        public void onLoadmore() {
            onLoadMord();
        }
    };

    private void onLoadMord() {
        limit = limit + 1;
        HashMap<String, String> map = new HashMap<>();
        map.put("p", limit + "");
        HttpCom.POST(CouponListHandler, HttpCom.getCouponList(), map, false);
    }

    private void initAndReflsh() {
        limit = 1;
        list.clear();
        HashMap<String, String> map = new HashMap<>();
        map.put("p", limit + "");
        HttpCom.POST(CouponListHandler, HttpCom.getCouponList(), map, false);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.right_text:
                startActivity(new Intent(this, MyCouponActivity.class));
                break;
        }
    }

    /**
     * 获取优惠券列表
     */
    @SuppressLint("HandlerLeak")
    private Handler CouponListHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            springView.onFinishFreshAndLoad();
            switch (msg.what) {
                case 1:
                    try {
                        Log.e("获取优惠券列表返回的数据:", msg.obj.toString());
                        CouponListBean colderBean = new Gson().fromJson(msg.obj.toString(), CouponListBean.class);
                        if (colderBean.getData().size() > 0) {
                            if (colderBean.getStatus() == 1) {
                                list.addAll(colderBean.getData());
                                couponAdapter.setListCoupon(list);
                            }
                        } else {
                            ToastUtil.showToast("还没有优惠券哦~");
                        }
                    } catch (Exception e) {
                        ToastUtils.showShortToast("数据解析异常");
                        Log.e("获取优惠券列表异常：", e.toString());
                    }
                    break;
                case 2:
                    ToastUtils.showShortToast("网络错误");
                    break;
            }
        }
    };
}
